<?php
// Library files
include '../includes/cPlayer.php';
include '../includes/cMember.php';
include '../includes/cDatabase.php';
include '../includes/cDates.php';
include '../includes/variables.php';
include '../includes/cPage.php';

// Globals
define("TD_POINTS", "<td width='100' align='center'>");
define("TD_NAME", "<td width='300' align='left'>");
define("TD_NORMAL", "<td width='100' align='center'>");

$cPage = new page("Team Of The Month: Previous Winners");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();
	
if(@$_GET['form'] == "one")
{
	$cDates = new dates();

	$month = $cDates->getMonthNumber($_POST['month']);

	$startWeek = $cDates->getWeekFromDate($cDates->getStartDateForMonth($month), 'start');
	$endWeek = $cDates->getWeekFromDate($cDates->getEndDateForMonth($month), 'end');

	// Now sort the member scores out
	$cDatabase = new database();

	$connect = $cDatabase->connectToDatabase();
	$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
	$db = $cDatabase->selectDatabase($connect, $activeDatabase);

	$selectArray[0] = '*';
	$result = $cDatabase->selectFromDatabase($connect, 'memberlist', $selectArray, NULL, NULL, NULL, NULL);

	$finfo = mysqli_fetch_fields($result);
	$numMembers = mysqli_num_rows($result);

	for ($i = 0; $i < $numMembers; $i++)		// Do each member's team at a time
	{
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
	
		// Get all the data for this member and put it in the class
		$cMember[$i] = new member;

		$error |= $cMember[$i]->setMemberName($row['memberName']);
		$error |= $cMember[$i]->setTeamName($row['teamName']);
		$error |= $cMember[$i]->setMoneyPaid($row['moneyPaid']);
		$error |= $cMember[$i]->setActive($row['active']);
		$error |= $cMember[$i]->loadMemberPlayerInfo($row, $connect);
	}

	$memberMonthlyScore = array();
	$memberMonthlyScoreUnsorted = array();

	for ($i = 0; $i < $numMembers; $i++)
	{
		for ($week = $startWeek; $week <= $endWeek; $week++)
		{
			$memberMonthlyScore[$i] += $cMember[$i]->getScoreForWeek($week);
			$memberMonthlyScoreUnsorted[$i] += $cMember[$i]->getScoreForWeek($week);
		}
	}
	rsort($memberMonthlyScore);		// Sort in reverse order


	// We now have all the scores in order but we need to relate them to each memberTeam
	for ($i = 0; $i < $numMembers; $i++)
	{
		for ($j = 0; $j < sizeof($memberMonthlyScore); $j++)
		{	
			if ($memberMonthlyScoreUnsorted[$i] == $memberMonthlyScore[$j])		// We've found a match
			{
				if (!$flag)
					$cMember[$i]->setPosition(($j + 1), $thisWeek-1);	// Add one as this array starts at 0
				$flag++;
			}
		}
		$cMember[$i]->setMatchingScore($flag, $thisWeek-1);	// Indicates how many other members are on the same score
		$flag = 0;												// reset flag, it's a new member
	}


	// Display Scoreboard
	echo "<div align='center'>";
	echo "<table>";
	echo TD_NORMAL."<strong>Position At Week(".$endWeek.")</strong></td>";
	echo TD_NAME."<strong>Team Name</strong></td>";
	echo TD_NAME."<strong>Member Name</strong></td>";
	echo TD_POINTS."<strong>Total Points (".$cDates->getMonthName($thisMonth).")</strong></td>";
	for ($position = 1; $position <= $numMembers; $position++)
	{
		if ($position < 2)
			$colour = RED;
		else
			$colour = BLUE;
		// Find members from first to last being careful with members on the same points
		for ($memberIndex = 0; $memberIndex < $numMembers; $memberIndex++)
		{
			if ($cMember[$memberIndex]->getPosition($thisWeek-1) == $position)
			{	// We've found it
				echo "<tr>".TD_NORMAL.$colour.$cMember[$memberIndex]->getPosition($thisWeek-1);
				if ($cMember[$memberIndex]->getMatchingScore($thisWeek-1) > 1)
				{
					echo "=";
				}
				echo "</font></td>";
				
				echo TD_NAME.$colour.$cMember[$memberIndex]->getTeamName()."</font></td>";
				echo TD_NAME.$colour.$cMember[$memberIndex]->getMemberName()."</font></td>";
				echo TD_POINTS.$colour.$memberMonthlyScoreUnsorted[$memberIndex]."</font></td>";
			
				echo "</tr>";
			}
		}
	}
	echo "</table></div>";
}
else if(@$_GET['form2'] == "two")
{
	echo "form2";
}
else
{
	$cDates = new dates();

	$endMonth = $cDates->getEndMonth();
	if ($endMonth < $cDates->getStartMonth())
	{
		// It's next year
		$endMonth = $cDates->getEndMonth()+12;
	}

	?>
	<div align="center">
	<form action="<?php echo $_SERVER['PHP_SELF'] ?>?form=one" method="POST">
	  	<table border='0'>
			<tr><td colspan="2" align="center">
	    		<select name="month">
				<?php
				for ($month = $cDates->getStartMonth(); $month <= $endMonth; $month++)
				{
					if ($month > 12)
					{
						// It's next year
						$monthName = $cDates->getMonthName($month - 12);
					}
					else
					{
						$monthName = $cDates->getMonthName($month);
					}
	    		  	echo"<option value = '" .$monthName. "'>" .$monthName. "</option>";
				}
				?>
		        </select>
	    		<input name="selectMonthButton" type="submit" id="selectMember" value="Select Month">
				</td></tr>	
	  	</table>
	</form></div>
	
	<?php
}

echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>